package topcbl.taxi;

import java.util.ArrayList;
import java.util.HashMap;

import topcbl.adapter.ListTaxiAdapter;
import topcbl.data.TaxiBrand;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Layout;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class QuickCallDialog extends Dialog {
	Context context;
	public static String CITY[];
	// ---
	Button back;
	Spinner spinner;
	ListView listTaxi;
	ListTaxiAdapter taxiAdapter;

	//
	HashMap<String, ArrayList<TaxiBrand>> mListTaxiBrand;

	public QuickCallDialog(Context context) {
		super(context, R.style.Theme_AlertDialog);
		this.context = context;
		CITY = context.getResources().getStringArray(R.array.city_array);
		createListTaxiBrand();

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dialog_quickcall);
		// change title divider
		int titleDividerId = context.getResources().getIdentifier(
				"titleDivider", "id", "android");
		View titleDivider = getWindow().getDecorView().findViewById(
				titleDividerId);
		titleDivider.setBackgroundColor(context.getResources().
				getColor(
				R.color.orange_trans)); // change divider color
		int titleTextId = context.getResources().getIdentifier("title", "id",
				"android");
		TextView titleText = (TextView) getWindow().getDecorView()
				.findViewById(titleTextId);
		titleText.setTextColor(context.getResources().getColor(R.color.tone_white));
		titleText.setTypeface(Typeface.SERIF);
		titleText.setText(context.getString(R.string.quick_call_title));
		titleText.setGravity(Gravity.CENTER);
		// animation
		getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		// get view
		back = (Button) findViewById(R.id.dialog_goinhanh_btn);
		spinner = (Spinner) findViewById(R.id.dialog_goinhanh_spinner);
		listTaxi = (ListView) findViewById(R.id.dialog_goinhanh_listtaxi);

		// setup view
		back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, CITY);
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		spinner.setAdapter(spinnerAdapter);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				String city = "";
				switch (position) {
				case 0:
					city = "danang";
					break;
				case 1:
					city = "hue";
					break;
				case 2:
					city = "hanoi";
					break;
				}
				taxiAdapter = new ListTaxiAdapter(context,
						R.layout.row_quickcall_taxi, mListTaxiBrand.get(city));
				listTaxi.setAdapter(taxiAdapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		taxiAdapter = new ListTaxiAdapter(context, R.layout.row_quickcall_taxi,
				mListTaxiBrand.get("danang"));
		listTaxi.setAdapter(taxiAdapter);
		listTaxi.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ListTaxiAdapter.HolderTaxiView holder = (ListTaxiAdapter.HolderTaxiView) view
						.getTag();
				String phone = holder.getPhone().getText() + "";
				Uri uri = Uri.parse("tel:" + phone);
				Intent callTaxi = new Intent(Intent.ACTION_CALL, uri);
				context.startActivity(callTaxi);
			}
		});
	}

	/**
	 * Hard set phone number for quick call taxi
	 */
	public void createListTaxiBrand() {
		mListTaxiBrand = new HashMap<String, ArrayList<TaxiBrand>>();
		ArrayList<TaxiBrand> danang = new ArrayList<TaxiBrand>();
		danang.add(new TaxiBrand("mailinh", "0511868686",
				R.drawable.logo_mailinh));
		danang.add(new TaxiBrand("songthu", "0511363363",
				R.drawable.logo_songthu));
		danang.add(new TaxiBrand("tiensa", "0511838383", R.drawable.logo_tiensa));
		ArrayList<TaxiBrand> hue = new ArrayList<TaxiBrand>();
		hue.add(new TaxiBrand("mailinh", "054868686", R.drawable.logo_mailinh));
		hue.add(new TaxiBrand("ig", "054363363", R.drawable.logo_tiensa));
		hue.add(new TaxiBrand("songhuong", "054838383", R.drawable.logo_songthu));
		ArrayList<TaxiBrand> hanoi = new ArrayList<TaxiBrand>();
		hanoi.add(new TaxiBrand("mailinh", "04868686", R.drawable.logo_mailinh));
		hanoi.add(new TaxiBrand("thudo", "043633636", R.drawable.logo_songthu));
		hanoi.add(new TaxiBrand("tiensa", "043357335", R.drawable.logo_tiensa));
		mListTaxiBrand.put("danang", danang);
		mListTaxiBrand.put("hue", hue);
		mListTaxiBrand.put("hanoi", hanoi);
	}

}
